package org.yagnus.stats.summary.univariate;

/**
 * WeightedAverage is just average of value times it's weight.
 * 
 * \sum{x_i * w_i} / n
 * 
 * @author u
 * 
 */
public class WeightedAverage extends UnivariateWeightedSummaryStatistic<Double> {
	double total = 0;
	int cnt = 0;

	public WeightedAverage() {
		clear();
	}

	@Override
	public void clear() {
		total = 0;
		cnt = 0;
	}

	@Override
	public Double get() {
		if (cnt == 0)
			return null;

		return total / cnt;
	}

	@Override
	public void add(Double data, double weight) {

		++cnt;

		if (weight == 0d) {
			return;
		}

		total += data * weight;

	}

}
